package dyy.zc.note.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.bmob.v3.datatype.BmobFile;
import dyy.zc.note.R;
import dyy.zc.note.bean.NoteInfo;
import dyy.zc.note.listener.MyOnItemClickListener;
import dyy.zc.note.listener.OnItemClickListener;
import dyy.zc.note.utils.ImageLoaderUtils;
import dyy.zc.note.utils.Utils;

/**
 * Created by chuan on 2017/4/27.
 */

public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.MyViewHolder>{
    private Context context;
    private List<NoteInfo> list;
    private MyOnItemClickListener onItemClickListener;
    private boolean online = false;//登录为true

    public void setOnline(boolean online) {
        this.online = online;
    }

    public NoteAdapter(Context context, List<NoteInfo> list){
        this.context = context;
        this.list = list;
    }

    public void setList(List<NoteInfo> list) {
        this.list = list;
    }

    public void setOnItemClickListener(MyOnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_note,null),onItemClickListener);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.content.setText(list.get(position).getContent());
        holder.title.setText(list.get(position).getTitle());
        if (list.get(position).getType().equals("-1")){
            holder.timeStr.setText(list.get(position).getTimeStr());
        }else {
            holder.timeStr.setText(list.get(position).getCreatedAt());
        }
        if (online){//登录之后才有图片
            if (list.get(position).getImg()!=null){
                if (list.get(position).getImg().size() != 0){

                    Glide.with(context).load(list.get(position).getImg().get(0).getFileUrl())
                            .error(R.drawable.chat_xe_icon2_03)
                            .into(holder.img);

                }else {

                    holder.img.setVisibility(View.INVISIBLE);
                }
            }else {

                holder.img.setVisibility(View.INVISIBLE);
            }
        }else {
            holder.img.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public int getItemCount() {
        if (list == null || list.size() == 0){
            return 0;
        }
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private MyOnItemClickListener onItemClickListener;
        private ImageView img,delete;
        private TextView title,content,timeStr;
        private RelativeLayout rl;
        public MyViewHolder(View itemView,MyOnItemClickListener onItemClickListener) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.onItemClickListener = onItemClickListener;
            img = Utils.findViewsById(itemView,R.id.img);
            title = Utils.findViewsById(itemView,R.id.title);
            content = Utils.findViewsById(itemView,R.id.content);
            timeStr = Utils.findViewsById(itemView,R.id.timeStr);
            rl = Utils.findViewsById(itemView,R.id.rl_click);
            delete = Utils.findViewsById(itemView,R.id.delete);
            rl.setOnClickListener(this);
            delete.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener !=null){
                switch (v.getId()){
                    case R.id.rl_click:
                        onItemClickListener.onItemClick(rl,getAdapterPosition());
                        break;
                    case R.id.delete:
                        onItemClickListener.onDeleteClick(delete,getAdapterPosition());
                        break;
                }
            }

        }
    }
}
